<template>
	<el-select v-model="model" :disabled="!props.isEdit" clearable filterable :filter-method="filterMethod">
		<template #prefix>
			<el-icon>
				<ele-Guide />
			</el-icon>
		</template>
		<el-option v-for="item in curOptions" :key="item.id" :value="item.id" :label="item.name"
			:disabled="!item.isEnabled" />
	</el-select>
</template>

<script lang="ts" setup>
//科室下拉选择框
import PinyinMatch from 'pinyin-match'
import { ref, watch } from 'vue';
import { findOrgRoot, keShiList } from '/@/stores/baseInfo/';
import { SysOrgApi, SysOrgAllOutput } from '/@/api/bas/sysOrg';
import { isNull, clog } from '/@/utils';

defineOptions({ name: 'keShiSelect' });
const model = defineModel()

const props = defineProps({
	isEdit: {
		type: Boolean,
		default: true,
	},
	orgType: { // 科室类型（住院、门诊）
		type: String,
		default: "",
		required: true,
	},
	pid: { // 院区id
		type: Number,
		default: 0,
		required: true,
	},
})

const options = ref<SysOrgAllOutput[]>([]);

watch(() => props.orgType, (newValue) => {
	updateOptions();
});

watch(() => props.pid, (newValue) => {
	updateOptions();
});

const updateOptions = () => {
	model.value = undefined;
	// var rootOrg = findOrgRoot(props.pid); // 查找根节点
	// 根据院区, 住院/门诊 进行过滤
	options.value = keShiList.value.filter(item => item.type == props.orgType && (item.pid == props.pid || findOrgRoot(item.pid).id == props.pid)) ?? [];
	filterMethod("");
}

// 过滤后的数据源 
const curOptions = ref<SysOrgAllOutput[]>([])
const filterMethod = (query: string) => {
	if (!isNull(query)) {
		curOptions.value = options.value.filter(item => {
			let values = JSON.stringify(item)
			return values.toLowerCase().indexOf(query.toLowerCase()) > -1 || PinyinMatch.match(values, query)
		})
	} else {
		curOptions.value = options.value
	}
}

</script>

<style lang="scss" scoped></style>
